package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.logic.cross.CrossUnionActivityRankListModel;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.LinkedList;

@Cmd(code = CrossProtocol.C2_CROSS_UNION_ACTIVITY_RANK, desc = "获取商会榜单")
public class GetCrossUnionActivityRankListCmd extends CrossCommand {
    private long userId;
    private int activityId;
    private String unionId;
    private String value;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossUnionActivtyRankReqMsg req = CrossActivityProto.CrossUnionActivtyRankReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = req.getActivtyId();
        unionId = req.getUnionId();
        value = req.getLocalValue();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo.getType() == eActivityType.UnionRankActivity.getValue()) {
            UnionActivityRankProto.UnionActivityRankListRespMsg.Builder respMsg = UnionActivityRankProto.UnionActivityRankListRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setMyRank(-1);
            respMsg.setValue("0");
            CrossUnionActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityUnionRankListModel(activityId);
            if (null != rankListModel) {
                UnionActivityRankProto.UnionActivityRankListMsg.Builder msg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
                respMsg.setMyRank(rankListModel.getMyRank(unionId));
                for (CrossUnionActivityRank rank : rankListModel.getRankList()) {
                    if (rank.getUnionUid().equals(unionId)) {
                        value = rank.getValue().toString();
                    }
                    if (rank.isDel()) {
                        continue;
                    }
                    UnionActivityRankProto.UnionActivityRankTempMsg.Builder builder = UnionActivityRankProto.UnionActivityRankTempMsg.newBuilder();
                    builder.setUnionUid(rank.getUnionUid());
                    builder.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(rank.getUnionBaseInfo()));
                    builder.setValue(rank.getValue().toString());
                    msg.addRankList(builder);
                }
                respMsg.setRankList(msg);
                respMsg.setValue(value);
            }
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_UNION_ACTIVITY_RANK_LIST, respMsg);
            MessageHelper.sendPacket(serverId, userId, message);
        } else {
            CrossUnionActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityUnionRankListModel(activityId);
            CrossActivityProto.CrossActivityUnionRankListRespMsg.Builder respMsg = CrossActivityProto.CrossActivityUnionRankListRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setMyRank(-1);
            if (rankListModel != null) {
                respMsg.setMyRank(rankListModel.getMyRank(unionId));
                respMsg.setRankList(rankListModel.getRankListMsg());
                for (CrossUnionActivityRank rank : rankListModel.getRankList()) {
                    if (rank.getUnionUid().equals(unionId)) {
                        value = rank.getValue().toString();
                    }
                }
            } else {
                CrossActivityProto.CrossActivityUnionRankListMsg.Builder builder = CrossActivityProto.CrossActivityUnionRankListMsg.newBuilder();
                builder.setActivityId(activityId);
                respMsg.setRankList(builder);
            }
            respMsg.setValue(value);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_ACTIVITY_UNION_RANK_LIST, respMsg);
            MessageHelper.sendPacket(serverId, userId, message);
        }
    }

    @Override
    public void sendData() {

    }
}
